package com.wzh.musichome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzh.musichome.common.ConstantCode;
import com.wzh.musichome.common.ResponseTemplate;
import com.wzh.musichome.entity.dto.CommentDTO;
import com.wzh.musichome.entity.dto.LikeDTO;
import com.wzh.musichome.entity.pojo.Comment;
import com.wzh.musichome.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author wzh
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;


    @PostMapping("/add")
    public Object addComment(@RequestBody CommentDTO commentDTO){
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        if(commentDTO.getSongId()!=null&&commentDTO.getSongId().equals("")){
            comment.setSongId(null) ;
        }else {
            comment.setSongId(commentDTO.getSongId());
        }
        if(commentDTO.getSongListId()!=null&&commentDTO.getSongListId().equals("")){
            comment.setSongListId(null);
        }else {
            comment.setSongListId(commentDTO.getSongListId());
        }
        try {
            boolean b = commentService.save(comment);
            if (b) {
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("评论成功");
                return data;
            }
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_INSERT, "评论失败！");
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_INSERT, "评论失败！！ 具体原因" + e.getMessage());
        }
    }

    /**
     * 修改评论
     */
    @PostMapping("/update")
    public Object updateComment(@RequestBody CommentDTO commentDTO){

        //保存到评论的对象中
        Comment comment = new Comment();
       BeanUtils.copyProperties(commentDTO,comment);
        if(commentDTO.getSongId()!=null&&commentDTO.getSongId().equals("")){
           comment.setSongId(null) ;
        }else {
            comment.setSongId(commentDTO.getSongId());
        }
        if(commentDTO.getSongListId()!=null&&commentDTO.getSongListId().equals("")){
           comment.setSongListId(null);
        }else {
            comment.setSongListId(commentDTO.getSongListId());
        }
        try {
            boolean b = commentService.saveOrUpdate(comment);
            if (b) {
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("修改成功");
                return data;
            }
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_UPDATE, "修改失败！");
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_UPDATE, "修改失败！！ 具体原因" + e.getMessage());
        }
    }


    @GetMapping("/delete/{id}")
    public Object selectByPrimaryKey(@PathVariable int id){
        try {
            boolean b = commentService.removeById(id);
            if (b){
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("删除成功");
                return data;
            }
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_DELETE, "删除失败！！");
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_DELETE, "删除失败！！ 具体原因" + e.getMessage());
        }
    }

    /**
     * 查询所有评论
     */
    @GetMapping("/allComment")
    public Object allComment(){
        try {
            List<Comment> list = commentService.list();
            ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
            data.setMsg("查找成功");
            data.setData(list);
            return data;
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_QUERY, "查找失败！！ 具体原因" + e.getMessage());
        }
    }

    /**
     * 查询某个歌曲下的所有评论
     */
    @GetMapping("/commentOfSongId/{songId}")
    public Object commentOfSongId(@PathVariable int songId){
        try {
            QueryWrapper<Comment> wrapper = new QueryWrapper<>();
            wrapper.eq("song_id",songId);
            List<Comment> list = commentService.list(wrapper);
            ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
            data.setMsg("查找成功");
            data.setData(list);
            return data;
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_QUERY, "查找失败！！ 具体原因" + e.getMessage());
        }
    }

    /**
     * 查询某个歌单下的所有评论
     */
    @GetMapping("/commentOfSongListId/{songListId}")
    public Object commentOfSongListId(@PathVariable int songListId){
        try {
            QueryWrapper<Comment> wrapper = new QueryWrapper<>();
            wrapper.eq("song_list_id",songListId);
            List<Comment> list = commentService.list(wrapper);
            ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
            data.setMsg("查找成功");
            data.setData(list);
            return data;
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_QUERY, "查找失败！！ 具体原因" + e.getMessage());
        }
    }



    /**
     * 给某个评论点赞
     */
    @RequestMapping(value = "/like",method = RequestMethod.POST)
    public Object like(@RequestBody LikeDTO likeDTO){
        Comment comment = commentService.getById(likeDTO.getId());
        comment.setUp(likeDTO.getUp());
        try {
            boolean b = commentService.saveOrUpdate(comment);
            if (b) {
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("点赞成功");
                return data;
            }
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_UPDATE, "点赞失败！");
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_UPDATE, "点赞失败！！ 具体原因" + e.getMessage());
        }
    }
}
